/*    */ package com.cr.api.controller;
/*    */ 
/*    */ import com.cr.api.model.AddDebtTransactionModel;
import com.cr.api.model.DeleteCrModel;
import com.cr.api.vo.*;
import com.cr.business.domain.CrBondClue;
/*    */ import com.cr.business.domain.CrDebtTransaction;
/*    */ import com.cr.business.domain.CrEvidenceInfo;
         import com.cr.business.domain.CrPayInfo;
import com.cr.business.dto.CreditorRightsDto;
import com.cr.business.service.*;
/*    */
/*    */
/*    */ import com.cr.common.core.controller.BaseController;
/*    */ import com.cr.common.core.domain.AjaxResult;
import com.cr.common.utils.ConvertUtil;
/*    */ import com.cr.common.utils.MyStringUtils;
import com.github.pagehelper.PageInfo;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
/*    */
/*    */
/*    */
/*    */
/*    */ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
/*    */ import org.springframework.beans.factory.annotation.Autowired;
/*    */ import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
/*    */
/*    */
/*    */

/*    */
        @Api(description = "债权交易")
/*    */ @RestController
        @Scope
/*    */ @RequestMapping({"/api/transaction"})
        @CrossOrigin
/*    */ public class ApiDebtTransactionController extends BaseController {
/*    */   @Autowired
/*    */   private ICrDebtTransactionService crDebtTransactionService;
/*    */   
/*    */   @Autowired
/*    */   private ICrEvidenceInfoService evidenceInfoService;
/*    */   
           @Autowired
           private ICrPayInfoService payInfoService;
           
           @Autowired
           private ICrBondClueService crBondClueService;

           
/*    */   @PostMapping({"/releaseTransaction"})
/*    */   @ResponseBody
           @ApiOperation("发布债权交易")
/*    */   public AjaxResult releaseTransaction(@RequestBody AddDebtTransactionModel model) {
/* 41 */     if (model.getUserId() == null)
/* 42 */       return AjaxResult.error("用户id不能为空"); 
/* 44 */     CrDebtTransaction crDebtTransaction = new CrDebtTransaction();
/* 45 */     BeanUtils.copyProperties(model, crDebtTransaction);
/* 46 */     crDebtTransaction.setCreateBy(model.getUserId().toString());
/* 47 */     crDebtTransaction.setCreateTime(new Date());
    crDebtTransaction.setBondCode("ZQZR"+new SimpleDateFormat("yyyyMMdd").format(new Date())+(int)((Math.random()*9+1)*1000));

    /* 48 */    // String address = "";
///* 49 */     if (MyStringUtils.isNotEmpty(model.getProvince()))
///* 50 */       address = address + model.getProvince();
///* 52 */     if (MyStringUtils.isNotEmpty(model.getCity()))
///* 53 */       address = address + model.getCity();
///* 55 */     if (MyStringUtils.isNotEmpty(model.getArea()))
///* 56 */       address = address + model.getArea();
/* 58 */     //crDebtTransaction.setAddress(address);
/* 59 */     Integer i = Integer.valueOf(this.crDebtTransactionService.insertCrDebtTransaction(crDebtTransaction));
/* 60 */     if (i.intValue() > 0 && 
/* 61 */       MyStringUtils.isNotEmpty(model.getDebtorList())) {
/* 62 */
                List<AddDebtTransactionModel.AddDebtor> debtorList = model.getDebtorList();
/* 63 */       for (AddDebtTransactionModel.AddDebtor debtor : debtorList) {
/* 64 */         CrEvidenceInfo info = new CrEvidenceInfo();
                 BeanUtils.copyProperties(debtor,info);
/* 66 */         info.setClueId(crDebtTransaction.getTransactionId());
/* 68 */         info.setCreateTime(new Date());
/* 69 */         this.evidenceInfoService.insertCrEvidenceInfo(info);
/*    */       } 
/*    */     } 
/* 73 */     return toAjax(i.intValue());
/*    */   }
            /**
             * 确认完成
             */
            @ApiOperation("确认完成")
            @ApiImplicitParam(name = "id", value = "我的交易id")
            @PostMapping("/confirmCompletion")
            @ResponseBody
            public AjaxResult confirmCompletion(@RequestBody CrPayInfo param)
            {
                Long id = param.getId();
                CrPayInfo info = payInfoService.selectCrPayInfoById(id);
                if(info != null){
                    info.setStatus(2);
                    Integer i = payInfoService.updateCrPayInfo(info);
                    return toAjax(i);
                }
                return AjaxResult.error("操作失败");
            }
            /**
             * 删除债权交易或线索
             */
            @ApiOperation("删除债权交易或线索")
            @ApiImplicitParam(name = "id", value = "债权id")
            @PostMapping("/deleteInfo")
            @ResponseBody
            public AjaxResult deleteInfo(@RequestBody DeleteCrModel param)
            {

                Long id = param.getId();
                Integer type = param.getType();
                if (id == null || type == null) {
                    return AjaxResult.error("id和type不能为空");
                }
                if (type == 1) {
                    Integer result = this.crBondClueService.deleteCrBondClueByClueId(id);
                    if (result > 0) {
                        return AjaxResult.success("删除成功");
                    }
                } else {
                    CrDebtTransaction debtTransaction = crDebtTransactionService.selectCrDebtTransactionByTransactionId(id);
                    if (debtTransaction != null) {
                        int i = crDebtTransactionService.deleteCrDebtTransactionByTransactionId(id);
                        if (i > 0) {
                            AjaxResult.success("删除成功");
                        }
                    }
                }
                return AjaxResult.error("删除失败");
            }
            /**
             * 债权交易详情
             */
            @ApiOperation("债权交易详情")
            @ApiImplicitParams({
                    @ApiImplicitParam(name = "transactionId", value = "id"),
                    @ApiImplicitParam(name = "type", value = "类型，1 债权线索。2 债权交易 ")
            })
            @PostMapping("/getInfo")
            @ResponseBody
            public AjaxResult getInfo(@RequestBody CrDebtTransaction param)
            {
                Long transactionId = param.getTransactionId(); 
                Integer type = param.getType();
                boolean miniInfo = false;
                if(StringUtils.isEmpty(param.getUserId())){
                    miniInfo = true;
                }else{
                    CrPayInfo info = new CrPayInfo();
                    info.setUserId(Long.parseLong(param.getUserId()));
                    info.setDataType(type.longValue());
                    info.setDataId(transactionId);
                    List<CrPayInfo> list = this.payInfoService.selectCrPayInfoList(info);
                    if(CollectionUtils.isEmpty(list)||list.get(0).getPayResult()==1)
                        miniInfo = true;
                }
                Map<String, Object> dataMap = new LinkedHashMap<>();
                ClassPathXmlApplicationContext d;

                if(type == 1){
                    CrBondClue bondClue = crBondClueService.selectCrBondClueByClueId(transactionId);
                    if(!param.getUserId().equalsIgnoreCase(bondClue.getCreateBy())&&miniInfo) {
                        MiniBondClueVo clueVo = new MiniBondClueVo();
                        BeanUtils.copyProperties(bondClue, clueVo);
                        dataMap.put("payResult","0");
                        dataMap.put("data",clueVo);
                        return AjaxResult.success(dataMap);
                    }
                    BondClueVo clueVo = new BondClueVo();
                    BeanUtils.copyProperties(bondClue, clueVo);
                    dataMap.put("payResult","1");
                    dataMap.put("data",clueVo);
                    return AjaxResult.success(dataMap);
                } else {
                    CrDebtTransaction debtTransaction = crDebtTransactionService.selectCrDebtTransactionByTransactionId(transactionId);
                    CrEvidenceInfo info = new CrEvidenceInfo();
                    info.setClueId(debtTransaction.getTransactionId());
                    List<CrEvidenceInfo> debtorList = this.evidenceInfoService.selectCrEvidenceInfoList(info);
                    List<CrDebtTransaction.AddDebtor> debtorList2 = new ArrayList<>();
                    for(CrEvidenceInfo evidenceInfo:debtorList){
                        CrDebtTransaction.AddDebtor debtor = new CrDebtTransaction.AddDebtor();
                        BeanUtils.copyProperties(evidenceInfo,debtor);
                        debtorList2.add(debtor);
                    }
                    debtTransaction.setDebtorList(debtorList2);
                    if(!param.getUserId().equalsIgnoreCase(debtTransaction.getCreateBy())&&miniInfo) {
                        MiniDebtTransactionVo clueVo = new MiniDebtTransactionVo();
                        BeanUtils.copyProperties(debtTransaction, clueVo);
                        dataMap.put("payResult","0");
                        dataMap.put("data",clueVo);
                        return AjaxResult.success(dataMap);
                    }
                    DebtTransactionVo vo = new DebtTransactionVo();
                    BeanUtils.copyProperties(debtTransaction, vo);
                    dataMap.put("payResult","1");
                    dataMap.put("data",vo);
                    return AjaxResult.success(dataMap);
                }

            }
            
            @ApiOperation("我的线索列表")
            @ApiImplicitParams({
                    @ApiImplicitParam(name = "userId", value = "用户id"),
            })
            @PostMapping("/getMineClueList")
            @ResponseBody
            public AjaxResult getMineClueList(@RequestBody CrBondClue param){
                String userId = param.getUserId();
                CrBondClue bondClue = new CrBondClue();
                bondClue.setCreateBy(userId);
                startPage();
                List<CrBondClue> crBondClueList = crBondClueService.selectCrBondClueList(bondClue);
                List<BondClueVo> vo = ConvertUtil.convertList(crBondClueList, BondClueVo.class);
                for(BondClueVo entity : vo){
                    entity.setTime(entity.getCreateTime());
                    entity.setTitle("债权线索");
                }
                PageInfo info = new PageInfo(vo);
                return AjaxResult.success(info);
            }
            
            /**
             * 我的债权列表
             * @return
             */
            @ApiOperation("我的债权列表 包括债权线索和交易")
            @ApiImplicitParams({
                    @ApiImplicitParam(name = "userId", value = "用户id"),
            })
            @PostMapping("/getMineList")
            @ResponseBody
            public AjaxResult getMineList(@RequestBody CrDebtTransaction param){
                String userId = param.getUserId();
                Map<String, Object> paramMap = new HashMap<>();
                paramMap.put("createdBy", userId);
                startPage();
                List<CreditorRightsDto> creditorRightsDtoList = this.crBondClueService.selectList(paramMap);
                PageInfo info = new PageInfo(creditorRightsDtoList);
                return AjaxResult.success(info);
            }
            
            /**
                                  * 我的交易
             * @return
             */
            @ApiOperation("我的交易")
//            @ApiImplicitParams({
//                    @ApiImplicitParam(name = "userId", value = "用户id"),
//            })
            @PostMapping("/getTransactionList")
            @ResponseBody
            public AjaxResult getTransactionList(@RequestBody CrPayInfo info){
//                CrPayInfo info = new CrPayInfo();
//                info.setUserId(userId);
                startPage();
                List<CrPayInfo> payInfoList = payInfoService.selectCrPayInfoList(info);
                List<TransactionVo> voList = new ArrayList<>();
                for(CrPayInfo payInfo : payInfoList){
                    if(payInfo.getDataType() == 1){
                        CrBondClue bondClue = crBondClueService.selectCrBondClueByClueId(payInfo.getDataId());
                        TransactionVo vo = new TransactionVo();
                        vo.setId(payInfo.getDataId());
                        vo.setType(payInfo.getDataType());
                        vo.setDateId(payInfo.getDataId());
                        vo.setTime(payInfo.getPayTime() != null ? payInfo.getPayTime() : payInfo.getCreateTime());
                        vo.setTitle("债权线索");
                        vo.setTransferAmount(new BigDecimal(0));
                        vo.setCreditorRightsAmount(bondClue.getRewardAmount());
                        vo.setStatus(payInfo.getStatus());
                        voList.add(vo);
                    }
                    if(payInfo.getDataType() == 2){
                        CrDebtTransaction debtTransaction = crDebtTransactionService.selectCrDebtTransactionByTransactionId(payInfo.getDataId());
                        TransactionVo vo = new TransactionVo();
                        vo.setId(payInfo.getDataId());
                        vo.setType(payInfo.getDataType());
                        vo.setDateId(payInfo.getDataId());
                        vo.setTime(payInfo.getPayTime() != null ? payInfo.getPayTime() : payInfo.getCreateTime());
                        vo.setTitle("个人转让债权");
                        vo.setTransferAmount(debtTransaction.getTransferAmount());
                        vo.setCreditorRightsAmount(debtTransaction.getDebtAmount());
                        vo.setStatus(payInfo.getStatus());
                        voList.add(vo);
                    }
                }
                return AjaxResult.success(new PageInfo<>(voList));
            }
            
            /**
             * 删除我的交易
             */
            @ApiOperation("删除我的交易")
            @ApiImplicitParam(name = "id", value = "我的交易id")
            @PostMapping("/payDelete")
            @ResponseBody
            public AjaxResult payDelete(@RequestBody CrPayInfo param)
            {
                Long id = param.getId();
                CrPayInfo info = payInfoService.selectCrPayInfoById(id);
                if(info != null){
                    Integer i = payInfoService.deleteCrPayInfoById(info.getId());
                    return toAjax(i);
                }
                return AjaxResult.error("操作失败");
            }
            
            /**
             * 修改债权交易
             */
            @ApiOperation("修改债权交易")
            @PostMapping("/updateInfo")
            @ResponseBody
            public AjaxResult updateInfo(@RequestBody AddDebtTransactionModel model)
            {
                CrDebtTransaction debtTransaction = crDebtTransactionService.selectCrDebtTransactionByTransactionId(model.getTransactionId());
                if(debtTransaction != null){
                    BeanUtils.copyProperties(model, debtTransaction);
                    debtTransaction.setUpdateTime(new Date());
                    debtTransaction.setUpdateBy(model.getUserId().toString());
                    Integer i = crDebtTransactionService.updateCrDebtTransaction(debtTransaction);
                    return toAjax(i);
                }
                return AjaxResult.error("修改失败");
            }
/*    */}


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-api-4.7.0.jar!\com\cr\api\controller\ApiDebtTransactionController.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */